<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!defined ('EXPLAY')) exit;

Templater::load ('users/people.tpl');

if (isset($_GET['cat']) && $_GET['cat'] == 'online') {
	$get = $db->query ("SELECT uid, usr.user_name, usr.user_karma, usr.user_login FROM ".DB_PEREFIX."_sessions AS sess
		LEFT JOIN (SELECT user_id, user_name, user_karma, user_login FROM ".DB_PEREFIX."_users) AS usr ON usr.user_id = sess.uid
		WHERE uid != 0 AND ugroup = 'user' ORDER BY utime DESC");
	
	if ($db->num_rows($get) > 0) {
		$karmas = array ();
		$users = array ();
		while (list($uid, $name, $karma, $login) = $db->fetch_row ($get)) {
			if ($karma < 0) $karma = 0;
			array_push ($karmas, $karma);
			array_push ($users, array ($uid, $name, $login));
		}
		
		for ($i=0; $i<count($users); $i++) {
			$karma = $karmas[$i];
			list ($uid, $name, $login) = $users[$i];
			
			$size = 31;
			for ($c=0; $c<count($users); $c++) if ($karmas[$c] > $karma) $size -= 3;
			
			$list .= "\n<a href=\"/users/{$login}\" style=\"font-size: {$size}px;\">$name</a> ";
		}
		
		past_table ('Пользователи on-line', $list);

	}
	else site_error ('На сайте нет пользователей!');



} else {
	$number = 20;
	$start = 0;
	if (!empty($_GET['page'])) $start = (intval ($_GET['page']) - 1) * $number;
	
	
	//
	// Последние он-лайн
	//
	$list = '';
	
	$get_online = $db->query ("SELECT utime, uid, page, u.user_name, u.user_login FROM ".DB_PEREFIX."_sessions AS s
		LEFT JOIN (SELECT user_id, user_name, user_login FROM ".DB_PEREFIX."_users) AS u ON u.user_id = s.uid
		WHERE ugroup = 'user' ORDER BY utime DESC");
	
	while (list($time, $id, $page, $user_name, $login) = $db->fetch_array($get_online)) {
		if (time() - $time < 60) {
			for ($i=0; $i<=60; $i++) {
					if (time() - $time == $i) {
						if (mb_strlen($i) == 1) {
							
							if (substr($i, mb_strlen($i)-1, mb_strlen($i)) == 1)
								$ptime = "$i секунду назад";
							elseif (substr($i, mb_strlen($i)-1, mb_strlen($i)) == (2 || 3 || 4))
								$ptime = "$i секунды назад";
							else
								$ptime = "$i секунд назад";
								
						}
						else {
							
							if (substr($i, mb_strlen($i)-2, mb_strlen($i)) == 1 && substr($i, mb_strlen($i)-2, mb_strlen($i)) != (11 || 12 || 13 || 14))
								$ptime = "$i секунду назад";
							elseif (substr($i, mb_strlen($i)-2, mb_strlen($i)) == (2 || 3 || 4) && substr($i, mb_strlen($i)-2, mb_strlen($i)) != (11 || 12 || 13 || 14))
								$ptime = "$i секунды назад";
							else
								$ptime = "$i секунд назад";
								
						}
					}
				}
		}
		elseif (time() - $time <= 900 && time() - $time >= 60) {
			for ($i=60; $i<=900; $i+=60) {
				if (time() - $time >= $i && time() - $time < ($i+60)) {
						$st = round ($i/60);
						if (mb_strlen($st) == 1) {
							
							if (substr($st, mb_strlen($st)-1, mb_strlen($st)) == 1)
								$ptime = "$st минуту назад";
							elseif (substr($st, mb_strlen($st)-2, mb_strlen($st)) == (2 || 3 || 4))
								$ptime = "$st минуты назад";
							else
								$ptime = "$st минут назад";
								
						}
						else {
							
							if (substr($st, mb_strlen($st)-2, mb_strlen($st)) == 1 && substr($st, mb_strlen($st)-2, mb_strlen($st)) != (11 || 12 || 13 || 14))
								$ptime = "$st минуту назад";
							elseif (substr($st, mb_strlen($st)-2, mb_strlen($st)) == (2 || 3 || 4) && substr($st, mb_strlen($st)-2, mb_strlen($st)) != (11 || 12 || 13 || 14))
								$ptime = "$st минуты назад";
							else
								$ptime = "$st минут назад";
							
						}
				}
			}
		}
		else $ptime = format_time ($time);
		
		Templater::set_var ('id', $id);
		Templater::set_var ('page', $page);
		Templater::set_var ('time', $ptime);
		Templater::set_var ('name', $user_name);
		Templater::set_var ('login', $login);
		
		$list .= Templater::display_part ('user_online', false);
		
	}
	
	//
	// Рейтинг
	//
	$get = $db->query ("SELECT user_id, user_name, user_date, user_karma, user_login FROM  ".DB_PEREFIX."_users WHERE user_ban = 'off' ORDER BY user_karma DESC LIMIT $start, $number");
	
	$get_count = $db->query ("SELECT COUNT(*) AS cnt FROM ".DB_PEREFIX."_users WHERE user_ban = 'off'");
	$get_count = $db->fetch_array ($get_count);
	$count_users = $get_count['cnt'];
	
	$block = '';
	
	while ($user = $db->fetch_array($get)) {
		
		$user['user_karma'] = number_format($user['user_karma'], 2, ',', '');
		
		Templater::set_var ('user', $user);
		$block .= Templater::display_part ('user_li', false);
		
	}
	
	if ($count_users > $number) $block .= past_pages($start, $number, $count_users, '/people/');
	

	//
	// Выбираем новых пользователей
	//
	$get_last = $db->query ("SELECT user_id, user_name, user_date, user_login FROM ".DB_PEREFIX."_users ORDER BY user_id DESC LIMIT 0, 20");
	$new_users = '';
	while (list ($id, $name, $date, $login) = $db->fetch_array ($get_last)) {
		Templater::set_var ('id', $id);
		Templater::set_var ('name', $name);
		Templater::set_var ('login', $login);
		Templater::set_var ('date', format_time ($date));
		
		$new_users .= Templater::display_part ('new_user', false);
	}
	
	//
	// Выводим
	//
	Templater::set_var ('online_list', $list);
	Templater::set_var ('block', $block);
	Templater::set_var ('new_users_list', $new_users);
	
	print Templater::display_part ('main');
	
	unset ($list, $block, $new_users);
}

